package 富婆梦.第一阶段.Java基础视频学习.线程的练习.作业7;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.线程的练习.作业7
 * @ClassName: Factory
 * @Author: hemengjin
 * @Description: 工厂类
 * 属性：库存
 * 方法：
 * 1、生产方法
 * 2、搬运方法
 * @Date:2021/11/7 11:53
 * @Version: 1.0
 */
public class Factory {
    //boolean isHave = false;
    int computer_count = 0;
    Lock lock = new ReentrantLock();

    //生产方法，如果没有库存就可以生产，有库存则等待
    public void produce() {
        //使用lock方法
        /*while (computer_count>0) {
            lock.lock();
            if (computer_count>0){
                try {
                    System.out.println(Thread.currentThread().getName() + "  " + "现在仓库里面还有电脑" + computer_count + "台，暂不生产");
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }
        if (computer_count==0) {
            try {
                lock.lock();
                computer_count = computer_count + 1;
                System.out.println(Thread.currentThread().getName() + "  " + "生产了一台电脑,库存为：" + computer_count);
            } finally {
                lock.unlock();
            }
        }*/
        while (true) {
            lock.lock();
            if (computer_count > 0) {
                try {
                    System.out.println(Thread.currentThread().getName() + "  " + "现在仓库里面还有电脑" + computer_count + "台，暂不生产");
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                    break;
                }
            } else {
                try {
                    computer_count = computer_count + 1;
                    System.out.println(Thread.currentThread().getName() + "  " + "生产了一台电脑,现在库存为：" + computer_count);
                } finally {
                    lock.unlock();
                    break;
                }
            }
        }
    }

    //搬运方法，有库存就可以搬运，无库存则等待
    public void move() {
        //使用lock方法
        /*while (computer_count == 0) {
            try {
                lock.lock();
                if (computer_count == 0) {
                    System.out.println(Thread.currentThread().getName() + "  " + "现在仓库里面库存是" + computer_count + "台，暂不搬运");
                    Thread.sleep(20);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
        try {
            lock.lock();
            if (computer_count > 0) {
                computer_count = computer_count - 1;
                System.out.println(Thread.currentThread().getName() + "  " + "搬运一台电脑,库存为：" + computer_count);
            }
        } finally {
            lock.unlock();
        }*/


        while (true) {
            lock.lock();
            if (computer_count == 0) {
                try {
                    System.out.println(Thread.currentThread().getName() + "  " + "现在仓库里面库存是" + computer_count + "台，暂不搬运");
                    Thread.sleep(200);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                    break;
                }
            } else {
                try {
                    computer_count = computer_count - 1;
                    System.out.println(Thread.currentThread().getName() + "  " + "搬运了一台电脑,现在库存为：" + computer_count);
                } finally {
                    lock.unlock();
                    break;
                }
            }


        }
    }
}
